require(['./config'],()=>{
    require(['template','header','footer','jquery'],(template,Header)=>{
       class order{
            constructor(){
                this.rendertable()
                if(this.cart){
                    this.setAllCheckStatus()
                    this.calcTotalMoney()
                    this.addsub()
                    this.checksChange() 
                    this.allcheck()
                    this.del()
                }   
            }
            rendertable(){
                let cart = localStorage.getItem('cart')
                if(cart){
                    $('#cart-empty').css({
                        'display':'none'
                    })
                    this.cart = JSON.parse(cart)
                    $('#order-list').html(
                        template('order-list-Template',{
                            cart:this.cart
                        })
                    )
                }else{
                    $('#cart-empty').css({
                        'display':'block'
                    })
                }         
                this.checksChange()
                
            }
            setAllCheckStatus(){
                const isAllCheck = this.cart.every(shop=>shop.check)
                $('#all-checked').prop('checked',isAllCheck)
            }
            //点击那个最重要的all-checked
            allcheck(){
                const that = this
                $('#all-checked').on('change',function(){
                    if(this.checked){
                        console.log('123')
                        // let cart = localStorage.getItem('cart')
                        // that.cart = JSON.parse(cart)
                        that.cart.map((item)=>{
                            item.check = true
                        })
                        localStorage.setItem('cart',JSON.stringify(that.cart))
                    }else{
                        console.log('456')
                        // let cart = localStorage.getItem('cart')
                        // that.cart = JSON.parse(cart)
                        that.cart.map(item=>{
                            item.check = false
                        })
                        localStorage.setItem('cart',JSON.stringify(that.cart))
                    }
                    that.setAllCheckStatus()
                    that.calcTotalMoney()
                    that.rendertable()
                })
            }
            checksChange(){
                const that = this
                $('.one-checked').on('change',function(){
                    //获取当前数据的id
                    const id = $(this).parents('tr').data('id')
                    //根据id修改值
                    that.cart = that.cart.map(shop => {
                        if(shop.id === id){
                            shop.check = $(this).prop('checked')
                        }
                        //无论条件是否成立，都要成立
                        return shop
                    })
                    localStorage.setItem('cart',JSON.stringify(that.cart))
                    that.calcTotalMoney()
                    that.setAllCheckStatus()
                }) 
            }
            //修改算钱就调用
            calcTotalMoney(){
                const allMoney = this.cart.reduce((money,shop)=>{
                    if(shop.check) money+=shop.price*shop.count
                    //无论条件是否满足都要写return
                    return money
                },0)
                $('#paymoney').html(allMoney.toFixed(2))
            }
            addsub(){
                const that = this
                $('.sub').on('click',function(){
                    let aa = $(this).siblings('.sum').val()-0
                    if(aa>=1){
                        aa--
                        $(this).siblings('.sum').val(aa)
                        let cart = localStorage.getItem('cart')
                        cart = JSON.parse(cart)                       
                        cart.map((item)=>{  
                            if(item.id===$(this).parents('tr').data('id')) {
                                item.count=aa
                                $(this).parents('tr').children('.order-price').html((aa*item.price).toFixed(2))
                            }
                        })
                        
                        let allmoney = cart.reduce((money,shop)=>{
                            if(shop.check) money+=shop.price*shop.count
                            //无论条件是否满足都要写return
                            return money
                        },0)
                        $('#paymoney').html(allmoney.toFixed(2))
                        localStorage.setItem('cart',JSON.stringify(cart))
                        Header.calcCartCount()
                    }
                    else{
                        alert('啥也不是')
                    }
                    
                })
                $('.plus').on('click',function(){
                    let aa = $(this).siblings('.sum').val()-0
                    if(aa>=0){
                        aa++
                        $(this).siblings('.sum').val(aa)
                        let cart = localStorage.getItem('cart')
                        cart = JSON.parse(cart)                       
                        cart.map((item)=>{   
                            if(item.id===$(this).parents('tr').data('id')) {
                                item.count=aa
                                $(this).parents('tr').children('.order-price').html((aa*item.price).toFixed(2))
                            }
                        })
                        
                        let allmoney = cart.reduce((money,shop)=>{
                            if(shop.check) money+=shop.price*shop.count
                            //无论条件是否满足都要写return
                            return money
                        },0)
                        $('#paymoney').html(allmoney.toFixed(2))
                        localStorage.setItem('cart',JSON.stringify(cart))
                        Header.calcCartCount()
                    }
                   
                })
                
       } 
       del(){
           const that = this
           $('.del').on('click',function(){
                const id = $(this).parents('tr').data('id')
                that.cart = that.cart.filter((item)=>{
                    return id !== item.id
                })
                console.log(that.cart)
                localStorage.setItem('cart',JSON.stringify(that.cart))
                that.calcTotalMoney()
                $(this).parents('tr').remove()
                if(that.cart.length === 0){
                    localStorage.removeItem('cart')
                    $('#cart-empty').css({
                        'display':'block'
                    }) 
                    Header.calcCartCount()
                }
                Header.calcCartCount()
                that.calcTotalMoney()
           })           
       }
            }
                
       new order()
    })
})